package com.mflex.mwms.adapter;

import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.mflex.mwms.R;
import com.mflex.mwms.activity.TransferOffActivity;
import com.mflex.mwms.model.CustomApiResult;
import com.mflex.mwms.model.Task;
import com.mflex.mwms.model.TaskDetail;
import com.mflex.mwms.model.TransferDetail;
import com.mflex.mwms.model.TransferDetailTaskRequest;
import com.mflex.mwms.model.UnBoxingPackInfo;
import com.mflex.mwms.utils.XToastUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.CallBackProxy;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.CollectionUtils;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.DialogAction;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.ArrayList;
import java.util.List;

public class TransferOffAdapter extends BaseAdapter {
    List<TaskDetail> items;
    Activity context;

    MiniLoadingDialog mMiniLoadingDialog;

    public TransferOffAdapter(Activity context, List<TaskDetail> items) {
        this.context = context;
        this.items = items;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TaskDetail item = items.get(position);
        View view = convertView;
        view = context.getLayoutInflater().inflate(R.layout.item_transfer_off_list, null);
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(context, "加载...");
        ((CheckBox) view.findViewById(R.id.checkBox)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new MaterialDialog.Builder(context)
                        .iconRes(R.drawable.icon_warning)
                        .title(R.string.tip_infos)
                        .content("是否确定删除")
                        .negativeText("取消")
                        .positiveText(R.string.lab_submit)
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                TransferDetailTaskRequest transferDetailTaskRequest = new TransferDetailTaskRequest();
                                List<TransferDetail> transferDetailList = new ArrayList<>();
                                TransferDetail transferDetail = new TransferDetail();
                                transferDetail.setBoxNumber(item.getDetailName());
                                transferDetail.setAciton(1);
                                transferDetailList.add(transferDetail);

                                transferDetailTaskRequest.setDetails(transferDetailList);
                                transferDetailTaskRequest.setTaskNo(((TextView) context.findViewById(R.id.stv_TaskNo)).getText().toString());

                                transferDetailTaskRequest.setDetails(transferDetailList);

                                mMiniLoadingDialog.show();
                                XHttp.post("api/Task/TransferDetailTask")
                                        .upJson(JsonUtil.toJson(transferDetailTaskRequest))
                                        .syncRequest(false)
                                        .onMainThread(true)
                                        .accessToken(true)
                                        .timeOut(120000)
                                        .execute(new CallBackProxy<CustomApiResult<List<Task>>, List<Task>>(new SimpleCallBack<List<Task>>() {

                                            @Override
                                            public void onSuccess(List<Task> response) {
                                                mMiniLoadingDialog.dismiss();
                                                XToastUtils.success("删除成功", 2500);

                                                items.remove(position);
                                                notifyDataSetChanged();
                                            }

                                            @Override
                                            public void onError(ApiException e) {
                                                mMiniLoadingDialog.dismiss();
                                                if (e.getCode() == 100) {
                                                    new MaterialDialog.Builder(context)
                                                            .iconRes(R.drawable.icon_warning)
                                                            .title(R.string.tip_warning)
                                                            .content(e.getMessage())
                                                            .positiveText(R.string.lab_submit)
                                                            .show();
                                                }
                                                if (e.getCode() == 200) {
                                                    new MaterialDialog.Builder(context)
                                                            .iconRes(R.drawable.icon_tip)
                                                            .title(R.string.tip_infos)
                                                            .content(e.getMessage())
                                                            .positiveText(R.string.lab_submit)
                                                            .show();
                                                }
                                            }
                                        }) {
                                        });
                            }
                        })
                        .show();
            }
        });
        ((TextView) view.findViewById(R.id.txtPackNo)).setText(item.getDetailName());
        ((TextView) view.findViewById(R.id.txtQty)).setText(item.getQty());

        if (position % 2 == 0) {
            ((LinearLayout) view.findViewById(R.id.txtList)).setBackgroundColor(Color.rgb(221, 221, 221));
        }

        return view;
    }
}